﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using T2.Logging.UI.MVC.Controllers;
using System.Web.Mvc;
using Microsoft.QualityTools.Testing.Fakes;
using System.Web.Security.Fakes;
using T2.Logging.UI.MVC.Models;
using T2.Logging.ASPMVC.Controllers;

namespace T2.WebSecurity.UI.MVC.Tests.Controllers
{
    [TestClass]
    public class AccountsController
    {
        [TestMethod]
        public void Login()
        {
            string testUserName = "trooks";
            string testPassword = "duminda098";
            bool testRememberMe = false;
            LoginModel model = new LoginModel();
            model.UserName = testUserName;
            model.Password = testPassword;
            model.RememberMe = testRememberMe;

            AccountController controller = new AccountController();
            using (ShimsContext.Create())
            {
                System.Web.Security.Fakes.ShimMembership.GetUser = () =>
                {
                    return new System.Web.Security.MembershipUser(
                        "T2MembershipProvider", "trooks", 1, "dumindapium@gmail.com", "car",
                        "none", true, false, Convert.ToDateTime("1/1/2013"), Convert.ToDateTime("1/1/2013"),
                        Convert.ToDateTime("1/10/2013"), Convert.ToDateTime("1/1/2013"), Convert.ToDateTime("1/1/2013"));
                };
                System.Web.Security.Fakes.ShimFormsAuthentication.SetAuthCookieStringBoolean = (userName, rememberMe) =>
                {
                    Assert.AreEqual(testUserName, userName);
                    Assert.AreEqual(testRememberMe, rememberMe);
                };
                System.Web.Security.Fakes.ShimRoles.GetRolesForUser = () =>
                {
                    return new string[] { "super admin" };
                };
                
                //WebMatrix.WebData.WebSecurity.Login(testUserName, testPassword, false);
                ActionResult result = controller.Login(model, "~/");
                //result.ExecuteResult(controller.ControllerContext);

                Assert.IsNotNull(result);
            }
        }

        [TestMethod]
        public void TestLogOff()
        {
            AccountController controller = new AccountController();
            var signOutCalled = false;
            RedirectToRouteResult redirectToRouteResult;

            using (ShimsContext.Create())
            {
                ShimFormsAuthentication.SignOut = () => 
                {
                    signOutCalled = true;
                };
                redirectToRouteResult = controller.LogOff() as RedirectToRouteResult;
                Assert.AreEqual(true, signOutCalled);
            }

            
            Assert.AreEqual("Index", redirectToRouteResult.RouteValues["Action"]);
            Assert.AreEqual("Home", redirectToRouteResult.RouteValues["controller"]);
        }

        [TestMethod]
        public void Confirm()
        {
            AccountController controller = new AccountController();

            ActionResult result = controller.Confirm("zEK9aNZJdBHJjHbJSBoqGg2");
            
        }

        [TestMethod]
        public void TestPasswordQuestions()
        {
            AccountController controller = new AccountController();

            ActionResult res = controller.PasswordQuestions();
            Assert.IsNotNull(res);
        }
    }
}
